import axios from 'axios'
import commonUrl from '../common/js/commonUrl.js'
// import {loadCityId} from '../common/js/cache.js'
import { getQueryString } from '../common/js/getQueryString.js'
import qs from 'qs'
import { loadCityCode, loadUserId, loadCity, loadPhoneName, loadVillageId } from '../common/js/cache'

const url = `${commonUrl.apihost}index.php/home/`

// 获取openid
export function getOpenId () {
  const url = `${commonUrl.apihost}index.php/home/login/login`
  const data = {
    code: getQueryString('code'),
    userId: loadUserId(),
    phone: loadPhoneName().phone ? loadPhoneName().phone : ''
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 如果是用户通过搜索进入的页面  传  openId  给后台
export function passOpenId (id) {
  const url = `${commonUrl.apihost}index.php/home/login/login`
  const data = {
    id: id,
    userId: loadUserId(),
    phone: loadPhoneName().phone ? loadPhoneName().phone : ''
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 获取首页公告，租售，求助列表
export function getVillageList (id) {
  return axios({
    method: 'GET',
    url: `${url}Index/recentAnnounce/?areas_id=${id}&city_code=${loadCityCode()}&city_name=${loadCity()}&user_id=${loadUserId()}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 通过微信jssdk 获取经纬度
export function getConfig () {
  const data = {
    url: window.location.href.split('#')[0]
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/wx/wx_jssdk/getSignPackage`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 通过经纬度获取城市，小区
export function getAreasId (lng, lat) {
  return axios({
    method: 'GET',
    url: `${url}areas/position?lat=${lat}&lng=${lng}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 小区公告的评论发送
export function senNoticeComment (text, arid) {
  const url = `${commonUrl.apihost}index.php/home/Article/commentArticle`
  const data = {
    articleId: arid,
    userId: loadUserId(),
    comment: text
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 小区公告的评论展示
export function noticeComment (id) {
  const url = `${commonUrl.apihost}index.php/home/Article/commentList`
  const data = {
    articleId: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 小区公告列表
export function noticeList (aid, type) {
  const url = `${commonUrl.apihost}index.php/home/article/announce`
  const data = {
    areas_id: aid,
    tag: type
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 结果公示总列表
export function resultList() {
  const url = `${commonUrl.apihost}index.php/home/Vote_result/publishList`
  const data = {
    userId: loadUserId(),
    areasId: loadVillageId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 公示搜索
export function serchTips(id, type, itemid, word) {
  const url = `${commonUrl.apihost}index.php/home/Vote_result/searchNumbering`
  const data = {
    userId: loadUserId(),
    areasId: loadVillageId(),
    id: id,
    type: type,
    itemId: itemid,
    numbering: word
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 选举公示列表
export function canList(id) {
  const url = `${commonUrl.apihost}index.php/home/Vote_result/publishCanResult`
  const data = {
    userId: loadUserId(),
    areasId: loadVillageId(),
    voteId: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 选举公示列表的查看详情
export function canDetail(id, name, uid) {
  const url = `${commonUrl.apihost}index.php/home/my_vote/getDetail`
  const data = {
    userId: uid,
    type: name,
    id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 事物公示列表
export function supList(id) {
  const url = `${commonUrl.apihost}index.php/home/Vote_result/publishSupResult`
  const data = {
    userId: loadUserId(),
    areasId: loadVillageId(),
    voteId: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 满意度公示列表
export function satList(id) {
  const url = `${commonUrl.apihost}index.php/home/Vote_result/publishSatResult`
  const data = {
    userId: loadUserId(),
    areasId: loadVillageId(),
    satId: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
